package com.song.controller;

import com.song.result.Result;
import com.song.service.FileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;

/**
 * @program: untitled1
 * @ClassName FileController
 * @description:
 * @author: 刘庆东
 * @create: 2025−01-08 16:05
 * @Version 1.0
 **/
@Api(tags = "阿里云文件管理")
@RestController
@RequestMapping("/api/oss/file")
public class FileController {

    @Autowired
    private FileService fileService;

    @ApiOperation("文件上传")
    @PostMapping("/upload")
    public Result upload(
            @ApiParam(value = "文件",required = true) @RequestParam("file") MultipartFile file, @ApiParam(value = "模块",required = false) @RequestParam("module") String module){

        try {
            InputStream inputStream = file.getInputStream();
            String originalFilename = file.getOriginalFilename();
            //module="images";

            String uploadUrl = fileService.upload(inputStream, module, originalFilename);

            return  Result.ok().message("上传成功了！").data("url",uploadUrl);

        } catch (Exception e) {
            e.printStackTrace();
        }

           return null;
    }

}
